package cc.rengu.igas.channel.cups.biz.impl;

import cc.rengu.igas.channel.cups.common.constant.CupsServiceConstant;
import cc.rengu.igas.channel.cups.facade.CupsManagerService;
import cc.rengu.igas.channel.cups.facade.request.CupsLineTestRequest;
import cc.rengu.igas.channel.cups.facade.request.CupsResetKeyRequest;
import cc.rengu.igas.channel.cups.facade.request.CupsSignInRequest;
import cc.rengu.igas.channel.cups.facade.request.CupsSignOutRequest;
import cc.rengu.igas.channel.cups.facade.response.CupsLineTestResponse;
import cc.rengu.igas.channel.cups.facade.response.CupsResetKeyResponse;
import cc.rengu.igas.channel.cups.facade.response.CupsSignInResponse;
import cc.rengu.igas.channel.cups.facade.response.CupsSignOutResponse;
import cc.rengu.igas.channel.cups.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author 王成
 * @date 2017/6/27
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class CupsManagerServiceImpl implements CupsManagerService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CupsSignInResponse> cupsSignIn(CupsSignInRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CupsSignInResponse> result = new Result<>();
        CupsSignInResponse cupsSignInResponse = new CupsSignInResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request, CupsServiceConstant.SIGN_IN_SERVICE, CupsSignInResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CupsSignInResponse) dubboResult.getResult());
        } catch (Exception e) {
            rglog.error("签到服务失败:<{}>", cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            result.setSuccess(false);
            if (e instanceof BizException) {
                cupsSignInResponse.setRespCode(((BizException) e).getCode());
                cupsSignInResponse.setRespDesc(e.getMessage());
            } else {
                cupsSignInResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                cupsSignInResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CupsSignOutResponse> cupsSignOut(CupsSignOutRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CupsSignOutResponse> result = new Result<>();
        CupsSignOutResponse cupsSignOutResponse = new CupsSignOutResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request, CupsServiceConstant.SIGN_OUT_SERVICE, CupsSignOutResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CupsSignOutResponse) dubboResult.getResult());
        } catch (Exception e) {
            rglog.error("签退失败:<{}>", cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            result.setSuccess(false);
            if (e instanceof BizException) {
                cupsSignOutResponse.setRespCode(((BizException) e).getCode());
                cupsSignOutResponse.setRespDesc(e.getMessage());
            } else {
                cupsSignOutResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                cupsSignOutResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CupsLineTestResponse> cupsLineTest(CupsLineTestRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CupsLineTestResponse> result = new Result<>();
        CupsLineTestResponse cupsLineTestResponse = new CupsLineTestResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request, CupsServiceConstant.LINE_TEST_SERVICE, CupsLineTestResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CupsLineTestResponse) dubboResult.getResult());
        } catch (Exception e) {
            rglog.error("线路测试失败:<{}>", cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            result.setSuccess(false);
            if (e instanceof BizException) {
                cupsLineTestResponse.setRespCode(((BizException) e).getCode());
                cupsLineTestResponse.setRespDesc(e.getMessage());
            } else {
                cupsLineTestResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                cupsLineTestResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CupsResetKeyResponse> resetCupsKey(CupsResetKeyRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CupsResetKeyResponse> result = new Result<>();
        CupsResetKeyResponse cupsResetKeyResponse = new CupsResetKeyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request, CupsServiceConstant.RESET_KEY_SERVICE, CupsResetKeyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CupsResetKeyResponse) dubboResult.getResult());
        } catch (Exception e) {

            rglog.error("申请重置密钥失败:<{}>", StringUtil.ExceptionToString(e));
            result.setSuccess(false);
            if (e instanceof BizException) {
                cupsResetKeyResponse.setRespCode(((BizException) e).getCode());
                cupsResetKeyResponse.setRespDesc(e.getMessage());
            } else {
                cupsResetKeyResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                cupsResetKeyResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
